<?php

namespace app\api\controller;

class Txapply
{
    public $pagesize = 10;

    /**
     * 用户提现接口
     */
    public function apply()
    {
        $auth = trim(input("request.auth"));//用户auth
        $money = trim(input("request.money"));//提现金额
        $ty = trim(input("request.ty"));//提现到哪里，0支付宝，1微信
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($money)) {
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '操作失败，参数错误[money]'];
            return json($return);
        }
        if($ty=="")
        {
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '操作失败，参数错误[ty]'];
            return json($return);
        }
        if($ty !="0" && $ty!="1"){
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '操作失败，参数错误[ty]'];
            return json($return);
        }

        if ($money > $userInfo['money']) {
            $return['err'] = 11001;
            $return['msg'] = '余额不够';
            return json($return);
        }


        $data = array(
            'uid' => $userInfo['id'],
            'zfb_name' => $userInfo['alipay_name'],
            'zfb_account' => $userInfo['alipay_account'],
            'tx_type' => 0,//0提现到支付宝  1提现到微信
            'ctime' => time(),
            'stats' => 0,
            'money' => $money,
        );
        //写一个事物来扣款
        $result = userMoneyTxApply($data);
        if ($result['err']==0) {
            $return = ['err' => 0, 'msg' => '操作成功', 'devMsg' => '操作成功'];
            return json($return);
        } else {
            return json($result);
        }
    }


    /**
     * 我的提现列表
     */
    public function lists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $w = array();
        $w['uid'] = $userInfo['id'];
        $w['is_delete'] = 0;
        $w['source'] = 0;
        $model = new \app\admin\model\Txapply();
        $lists = $model->lists($w, $this->pagesize);
        $lists = $lists['data'];
        $lists = \app\admin\mapper\Txapply::getInstance()->listMapper($lists);
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        $return['data'] = $lists;
        return json($return);
    }
}
